package com.ctshk.rpc.order.goods.mapper.provider;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.jdbc.SQL;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ctshk.rpc.order.goods.dto.OrderPageListDTO;
import com.ctshk.rpc.order.goods.req.OrderPageListReq;

public class OrderProvider {

    public String findOrderPageByCondition(IPage<OrderPageListDTO> page, OrderPageListReq pageReq) {
        String sql = new SQL() {{
            SELECT("t.id, t.order_number, t.order_amount, t.freight_amount, t.payment_status, t.user_id, t.user_name, t.order_notes, t.order_status, t.gmt_create, t.source_id, t.source_name, t.source_type,t.order_write_off_number,t.write_off_time, t.delivery_mode,t.write_off_status, t.is_deleted, t.create_id, " +
                    "oci.consignee,oci.consignee_phone,oci.consignee_address, oci.consignee_email, oci.consignee_area_id, oci.consignee_area_name, " +
                    "osi.shipper,osi.shipper_phone,osi.shipper_store_id,osi.shipper_store_name,osi.shipper_store_address,osi.shipper_date ");
            FROM("`ctshk-goods-order`.t_order t " +
                    "LEFT JOIN `ctshk-goods-order`.t_order_consignee_info oci ON (oci.order_id = t.id) " +
                    "LEFT JOIN `ctshk-goods-order`.t_order_shipper_info osi ON (osi.order_id = t.id)  ");
            WHERE("t.is_deleted = 0");
            if(pageReq.getOrderStatus() != null && pageReq.getOrderStatus() != 0){
                WHERE("t.order_status = " + pageReq.getOrderStatus());
            }
            if(pageReq.getUserIds() != null){
                WHERE("t.create_id in (${pageReq.userIds})");
            }
            if(pageReq.getShipperOrPhone() != null){
                WHERE("osi.shipper_phone = " + pageReq.getShipperOrPhone());
            }

            // 订单编号
            if (StringUtils.isNotBlank(pageReq.getOrderNumber())){
                WHERE("t.order_number like concat('%',#{pageReq.orderNumber},'%')");
            }
            // 订单下单时间
            if (null != pageReq.getGmtCreate()){
                WHERE("t.gmt_create >= #{pageReq.gmtCreate}");
            }
            if (pageReq.getDeliveryMode() != null && pageReq.getDeliveryMode() == 1) {
                WHERE("t.delivery_mode = 1"); // 配送方式，1-快递
                // 收货人或收货人电话
                if (StringUtils.isNotBlank(pageReq.getConsigneeOrPhone())) {
                    WHERE("(oci.consignee like concat('%',#{pageReq.consigneeOrPhone},'%')) OR (oci.consignee_phone like concat('%',#{pageReq.consigneeOrPhone},'%'))");
                }
                // 订单来源名称
                if (StringUtils.isNotBlank(pageReq.getSourceName())) {
                    WHERE("t.source_name like concat('%',#{pageReq.sourceName},'%')");
                }
                // 订单状态
                if (null != pageReq.getOrderStatus() && pageReq.getOrderStatus() > 0) {
                    WHERE("t.order_status like concat('%',#{pageReq.orderStatus},'%')");
                }
            } else if(pageReq.getDeliveryMode() != null && pageReq.getDeliveryMode() == 2){
                WHERE("t.delivery_mode = 2"); // 配送方式，2-门店自提
                // 提货人
                if (StringUtils.isNotBlank(pageReq.getShipperOrPhone())) {
                    WHERE("(osi.shipper like concat('%',#{pageReq.shipperOrPhone},'%')) OR (osi.shipper_phone like concat('%',#{pageReq.shipperOrPhone},'%'))");
                }
                // 訂單狀態
                if (null != pageReq.getWriteOffStatus() && pageReq.getWriteOffStatus() >0){
                    if(pageReq.getWriteOffStatus() == 1 || pageReq.getWriteOffStatus() == 2) {
                        WHERE("t.write_off_status >= #{pageReq.writeOffStatus}");
                    }
                } else if (null != pageReq.getOrderStatus() && pageReq.getOrderStatus() > 0) {
                    if(pageReq.getOrderStatus() == 5) {
                        WHERE("t.order_status >= #{pageReq.orderStatus}");
                    }
                }
                // 自提分社
                if (null != pageReq.getShipperStoreId()){
                    WHERE("osi.shipper_store_id >= #{pageReq.shipperStoreId}");
                }
            }
            ORDER_BY("gmt_create desc");
        }}.toString();
        return sql;
    }
}
